﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Demo.CameraMouse{
	public class test : MonoBehaviour {

		//////////////////////////////////////////////////绕一点旋转//////////////////////////////////////////////////
		float x;
		float y;
		float xSpeed = 150;
		float ySpeed = 150;
		float yminLimit = -60;
		float ymaxLimit = 180;

		//////////////////////////////////////////////////移动摄像机//////////////////////////////////////////////////
		float m_xSpeed = 5;
		float m_ySpeed = 5;
		private float targetHeight = 0;           //与目标物体的高度差，eg:1.7 摄像机比目标高1.7米
		public Transform target;
		public Transform cameraTrans;
		public Transform CloneTarget;

		//////////////////////////////////////////////////拉进，拉远摄像机//////////////////////////////////////////////////
		private float distance = 3.5f;               //摄像机与目标物体距离
		private float scrollSpeed = 5;				//缩放速度
		private float Minzoom = 2;					//缩放最小值
		private float Maxzoom = 8;					//缩放最大值

		private float distanceLerp;

		//////////////////////////////////////////////////摄像机放大，放小//////////////////////////////////////////////////
		

		private float fov_distance = 60f;               //摄像机与目标物体距离
		private float fov_scrollSpeed = 20;				//缩放速度
		private float fov_Minzoom = 2;					//缩放最小值
		private float fov_Maxzoom = 60;					//缩放最大值

		private float fov_distanceLerp = 60;

		void Start(){
			cameraTrans = Camera.main.transform;
			CloneTarget.position = target.position;
			CloneTarget.rotation = target.rotation;

			distanceLerp =  distance;
			
			y = 30;
			Quaternion rotation = Quaternion.Euler(y,0,0);
			transform.rotation = rotation;

			isHover = false;
		}


		bool isNear = false;
		void LateUpdate () {
			//if(!isHover) return;

			if(Input.GetMouseButton(0)){//鼠标左键 控制旋转
				x += Input.GetAxis("Mouse X") * xSpeed *0.02f;
				y -= Input.GetAxis("Mouse Y") * ySpeed *0.02f;

				y = ClampAngle (y,yminLimit,ymaxLimit);//超过 限定值，让y停留在限定值，从而不会导致Y一直增长
				Quaternion rotation = Quaternion.Euler(y /*ClampAngle(y,yMinLimit,yMaxLimit)*/,x,0);

				transform.rotation = rotation;
			}

			if(Input.GetMouseButton(1)){//鼠标右键 控制拖拽
				float x = Input.GetAxis("Mouse X") * m_xSpeed *0.02f;
				float y = Input.GetAxis("Mouse Y") * m_ySpeed *0.02f;
				Vector3 targetPos = CloneTarget.position + cameraTrans.right * x + cameraTrans.up * y;
				CloneTarget.position = targetPos;

			}
			

			float ScrollWheelValue =  Input.GetAxis("Mouse ScrollWheel") ;
			
			distance = Mathf.Lerp(distance,distanceLerp,Time.deltaTime * scrollSpeed);
			fov_distance = Mathf.Lerp(fov_distance,fov_distanceLerp,Time.deltaTime * fov_scrollSpeed);

			if(ScrollWheelValue != 0){ 
				Debug.LogWarning("0 ScrollWheelValue:"+ScrollWheelValue);
				if(!isNear){
					distanceLerp = Vector3.Distance (transform.position , CloneTarget.transform.position);
					//Debug.LogError("1 distanceLerp:"+distanceLerp);

					float t = distanceLerp - ScrollWheelValue*scrollSpeed;

					//Debug.LogError("2 distanceLerp:"+t);
					distanceLerp = Mathf.Clamp(t, Minzoom, Maxzoom);
					//Debug.LogError("3 distanceLerp:"+distanceLerp);
					isNear= (distance-Minzoom <=0.0001f && distanceLerp-Minzoom<=0.0001f);
					Debug.LogFormat("1 isNear:{0}  distance:{1:f6} distanceLerp:{2:f6}",isNear,distance,distanceLerp);
				}

				if(isNear){
	
					fov_distanceLerp = Camera.main.fieldOfView;
					Debug.LogError("2 fov_distanceLerp:"+fov_distanceLerp);
					float fov_t = fov_distanceLerp -ScrollWheelValue*fov_scrollSpeed ;
					Debug.LogError("3 fov_distanceLerp:"+fov_t);
					fov_distanceLerp = Mathf.Clamp(fov_t, fov_Minzoom, fov_Maxzoom);
					Debug.LogError("4 fov_distanceLerp:"+fov_distanceLerp);

					if(fov_Maxzoom-fov_distanceLerp<=0.0001f){
						isNear = false;
					}
				}
			}
			
			Camera.main.fieldOfView = fov_distance;
			Vector3 position = CloneTarget.position - (transform.rotation * Vector3.forward * distance + new Vector3(0, -targetHeight, 0));
			transform.position = position;
			
		}

		public static float ClampAngle (float angle, float min, float max)
		{
			if (angle < -360F)
				angle += 360F;
			if (angle > 360F)
				angle -= 360F;
			return Mathf.Clamp (angle, min, max);
		}

		bool isHover = false;
		public void OnPointEnter(){
			isHover = true;
		}
		public void OnPointExit(){
			isHover = false;
		}

		
	}
}
